import { ElMessage } from "element-plus";

export default class Assert {

    /**
     * 断言这个 boolean 为 true
     * <p>为 false 则抛出异常</p>
     *
     * @param expression boolean 值
     * @param message    消息
     */
    public static isTrue(expression: boolean, message: string, params: string[] = []) {
        if (!expression) {

            let index = 0;
            for (const iterator of params) {
                let reg = new RegExp("%s" + index, "g")
                message = message.replace(reg, params[index]);
                index++;
            }
            ElMessage.error({
                message: message,
                type: 'error',
                duration: 2000
              });
            // ErrorToastUnit.tipError(message);

            throw new Error(message);
        }
    }

    /**
     * 断言这个 boolean 为 false
     * <p>为 true 则抛出异常</p>
     *
     * @param expression boolean 值
     * @param message    消息
     */
    public static isFalse(expression: boolean, message: string, params: string[] = []) {
        this.isTrue(!expression, message, params);
    }

    /**
     * 断言这个 object 为 null
     * <p>不为 null 则抛异常</p>
     *
     * @param object  对象
     * @param message 消息
     */
    public static isNull(object: any, message: string, params: string[] = []) {
        this.isTrue(object == null, message, params);
    }

    /**
     * 断言这个 object 不为 null
     * <p>为 null 则抛异常</p>
     *
     * @param object  对象
     * @param message 消息
     */
    public static notNull(object: any, message: string, params: string[] = []) {
        this.isTrue(object != null, message, params);
    }

    /**
     * 断言这个 value 不为 empty
     * <p>为 empty 则抛异常</p>
     *
     * @param value   字符串
     * @param message 消息
     */
    public static notEmpty(value: any, message: string, params: string[] = []) {
        this.isFalse(value == null || value == "", message, params);
    }
    /**
     * 断言这个 value 为 empty
     * <p>为 empty 则抛异常</p>
     *
     * @param value   字符串
     * @param message 消息
     */
    public static isEmpty(value: any, message: string, params: string[] = []) {
        this.isTrue(value != null && value != "", message, params);
    }


}